local assets =
{
    Asset("ANIM", "anim/bboy_paper.zip"),
}

local function Beefalo_OnBurnt(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local fx = SpawnPrefab("spawn_fx_medium")
    fx.Transform:SetPosition(x, y, z)

    -- 等特效动画播完再做后续
    fx:ListenForEvent("onremove", function()
        -- 生成牛，并放在同一落点
        local beef = SpawnPrefab("bboy_paper_beefalo")
        if beef and beef:IsValid() then
            if beef.Physics then
                beef.Physics:Teleport(x, y, z)
            else
                beef.Transform:SetPosition(x, y, z)
            end
        end
        local players = FindPlayersInRange(x, y, z, 10, true)
        for _, p in ipairs(players) do
            beef.components.follower:SetLeader(p)
            break
        end
    end)
end

local function Effigy_OnBurnt(inst)
    local x, y, z = inst.Transform:GetWorldPosition()

    -- 查找范围内所有玩家
    local players = FindPlayersInRange(x, y, z, 10, true)

    local chosen_leader = nil
    for _, p in ipairs(players) do
        -- 统计该玩家已有多少纸人随从
        local count = 0
        if p.components.leader ~= nil then
            for follower, _ in pairs(p.components.leader.followers) do
                if follower.prefab == "bboy_paper_effigy" and follower:IsValid() then
                    count = count + 1
                end
            end
        end

        if count < 3 then
            chosen_leader = p
            break
        end
    end

    -- 如果找到合适的主人才生成
    if chosen_leader ~= nil then
        local effigy = SpawnPrefab("bboy_paper_effigy")
        if effigy and effigy:IsValid() then
            if effigy.Physics then
                effigy.Physics:Teleport(x, y, z)
            else
                effigy.Transform:SetPosition(x, y, z)
            end
            effigy.components.follower:SetLeader(chosen_leader)
        end
    end
end

local function MakePaper(name, OnBurntFn)
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddFollower()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("bboy_paper")
        inst.AnimState:SetBuild("bboy_paper")
        inst.AnimState:PlayAnimation(name)

        inst:AddTag("bboy_paper")

        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        inst:AddComponent("inspectable")

        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = TUNING.SMALL_FUEL

        inst:AddComponent("tradable")

        inst:AddComponent("inventoryitem")

        MakeSmallBurnable(inst, TUNING.SMALL_BURNTIME)
        MakeSmallPropagator(inst)

        -- 添加监听点燃完成的逻辑
        if OnBurntFn then
            inst:ListenForEvent("onburnt", OnBurntFn)
        end

        MakeHauntableLaunchAndIgnite(inst)

        return inst
    end

    return Prefab("bboy_paper_" .. name .. "_item", fn, assets)
end

return MakePaper("beefalo", Beefalo_OnBurnt),
    MakePaper("effigy", Effigy_OnBurnt)
